<?php

namespace App\Models\V1;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class Users extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $connection = "mysql";
    protected $table = "users";
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'community_id',
        'mobile',
        'username',
        'partition',
        'building',
        'hnum',
        'floor',
        'integral',
        'avatar',
        'state',
        'preparatory_id',
        'registration_id'
    ];


    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'token',
    ];


    public function Community()
    {
        return $this->hasOne(Community::class, 'id', 'community_id');
    }

    public function Preparatory()
    {
        return $this->hasOne(Preparatory::class, 'id', 'preparatory_id');
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'token';
    }

    /**
     * 获取会储存到 jwt 声明中的标识
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * 返回包含要添加到 jwt 声明中的自定义键值对数组
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return ['role' => 'box'];
    }

    public function getCreatedAtAttribute()
    {
        return date("Y-m-d H:i:s", strtotime($this->attributes['created_at']));
    }

    public function getUpdatedAtAttribute()
    {
        return date("Y-m-d H:i:s", strtotime($this->attributes['updated_at']));
    }

    //用户是否关注某人
    public function isFollow($followed_id = 0)
    {
        return $this->following()->where('followed_id', $followed_id)->count();
    }

    //用户关注
    public function following()
    {
        return $this->belongsToMany(self::class, 'users_follows', 'follower_id', 'followed_id')->withTimestamps();
    }

    //用户的粉丝
    public function followers()
    {
        return $this->belongsToMany(self::class, 'users_follows', 'followed_id', 'follower_id')->withTimestamps();
    }

    //关注用户
    public function followThisUser($user)
    {
        return $this->following()->toggle($user);
    }

    /**
     * 消息设置
     * @param $key
     * @return void
     */
    public function getMessageSettingAttribute($key)
    {
        if (empty($key)) {
            return ['activity' => 0, 'comment' => 0, "zan" => 0];
        }
        return json_decode($key);
    }

    /**
     * 头像处理
     * @param $key
     * @return void
     */
    public function getAvatarAttribute($key)
    {
        if (empty($key)) {
            return 'http://cdn.97soso.com/avatar/avatar.png';
        }
        return $key;
    }


}
